﻿using System;
using System.Collections.Generic;


namespace Effects.Effects.Tools
{
    /// <summary>
    ///  Simple class measuring signal's power by computing the RMS.
    /// </summary>
    public class RMS
    {

        /// <summary>
        ///  Attack time.
        /// </summary>
        private double time;

        /// <summary>
        ///  Weighting coefficient.
        /// </summary>
        private double rmsCoef;


        /// <summary>
        ///  Creates new instance with given time attack, and sampling rate.
        /// </summary>
        /// <param name="time"> Length of the meassurement time interval. </param>
        /// <param name="sampleRate"> Sampling rate of the input signal. </param>
        public RMS(double time, int sampleRate)
        {
            this.time = time;
            rmsCoef = 1 - Math.Exp(-2200 / (2 * (double)sampleRate));
        }

        /// <summary>
        ///  Previous output.
        /// </summary>
        private double lastOutput;

        /// <summary>
        ///  Computation method.
        /// </summary>
        /// <param name="input"> Sample of input signal</param>
        /// <returns> RMS value </returns>
        public double Step(double input)
        {
            lastOutput = (1 - rmsCoef) * lastOutput + rmsCoef * input * input;
            return Math.Sqrt(lastOutput);
        }
    }
}
